package cn.z2huo.demo.csv.hutool.service;

import cn.hutool.core.text.csv.*;
import cn.z2huo.demo.model.dataobject.user.UserDO;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * @author z2huo
 */
@Service
public class ReadCSVServiceImpl {

    public void readCsv() {

        CsvReadConfig csvReadConfig = CsvReadConfig.defaultConfig();
        csvReadConfig.setHeaderLineNo(0).setBeginLineNo(0);

        try (FileReader fileReader = new FileReader(ResourceUtils.getFile("classpath:csv/z2huo_user.csv"));
             CsvReader reader = CsvUtil.getReader(fileReader, csvReadConfig);) {

            CsvData read = reader.read();
            List<String> header = read.getHeader();
            System.out.println("header:" + header);

            int rowCount = read.getRowCount();
            System.out.println("rowCount:" + rowCount);

            List<CsvRow> rows = read.getRows();
            for (CsvRow row : rows) {
                long originalLineNumber = row.getOriginalLineNumber();
                int fieldCount = row.getFieldCount();
                Map<String, String> fieldMap = row.getFieldMap();

                System.out.println("curr line is: " + originalLineNumber + ", fieldCount is: " + fieldCount + ", file map is:" + fieldMap);

                List<String> rawList = row.getRawList();
                System.out.println("rawList is:" + rawList);

                String userName = row.getByName("user_name");
                System.out.println("userName is:" + userName);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void readCsv2UserDO() {

        CsvReadConfig csvReadConfig = CsvReadConfig.defaultConfig();
        csvReadConfig.setHeaderLineNo(0).setBeginLineNo(0);

        try (FileReader fileReader = new FileReader(ResourceUtils.getFile("classpath:csv/z2huo_user.csv"));
             CsvReader reader = CsvUtil.getReader(fileReader, csvReadConfig);) {

            reader.forEach(csvRow -> {
                long originalLineNumber = csvRow.getOriginalLineNumber();
                int fieldCount = csvRow.getFieldCount();
                Map<String, String> fieldMap = csvRow.getFieldMap();

                System.out.println("curr line is: " + originalLineNumber + ", fieldCount is: " + fieldCount + ", file map is:" + fieldMap);

                List<String> rawList = csvRow.getRawList();
                System.out.println("rawList is:" + rawList);

                System.out.println("first Element is: " + csvRow.getFirst());

                String userName = csvRow.getByName("user_name");
                System.out.println("userName is:" + userName);

                UserDO bean = csvRow.toBean(UserDO.class);
                System.out.println("bean is:" + bean);

                System.out.println();
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
